package main.database;

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Projections;
import com.mongodb.client.model.Updates;
import main.GlobalConfig;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class BookCommentHelper {
    // save
    public static String save(String userId, String title, String content, String id) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String createTime = now.format(formatter);

        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfBookComment);

        if (id.isEmpty()) {
            Document document = new Document("uuid", userId)
                    .append("title", title)
                    .append("content", content)
                    .append("createtime", createTime)
                    .append("updatetime", createTime)
                    .append("submit", false)
                    .append("submittime", createTime);
            collection.insertOne(document);
            id = document.get("_id").toString();
        }
        else {
            Bson filter = Filters.eq("_id", new ObjectId(id));
            Bson update = Updates.combine(Updates.set("title", title), Updates.set("content", content));
            collection.updateOne(filter, update);
        }

        return id;
    }

    // submit

    // create
    public static String submit(String userId, String title, String content, boolean submit, String id) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String createTime = now.format(formatter);

        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfBookComment);
        if (id.isEmpty()) {
            Document document = new Document("uuid", userId)
                    .append("title", title)
                    .append("content", content)
                    .append("createtime", createTime)
                    .append("updatetime", createTime)
                    .append("submit", submit)
                    .append("submittime", createTime);
            collection.insertOne(document);
            id = document.get("_id").toString();
        }
        else {
            Bson filter = Filters.eq("_id", new ObjectId(id));
            Bson update = Updates.combine(Updates.set("title", title), Updates.set("content", content), Updates.set("submittime", createTime), Updates.set("submit", true));
            collection.updateOne(filter, update);
        }

        return id;
    }

    // read

    /**
     *
     * @param uuid 用户的uuid
     * @return
     */
    public static List<String[]> getInfosFromUser(String uuid) {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfBookComment);
        FindIterable<Document> findIterable = collection.find(Filters.eq("uuid", uuid))
                .projection(Projections.include("title", "createtime", "updatetime", "submittime", "submit", "_id"));
        MongoCursor<Document> cursor = findIterable.iterator();

        List<String[]> resultList = new ArrayList<>();
        while(cursor.hasNext()) {
            Document doc = cursor.next();

            String[] values = new String[6];
            values[0] = doc.get("_id").toString();
            values[1] = doc.get("title").toString();
            values[2] = doc.get("createtime").toString();
            values[3] = doc.get("updatetime").toString();
            values[4] = doc.get("submittime").toString();
            values[5] = doc.get("submit").toString();

            resultList.add(values);
        }
        cursor.close();

        return  resultList;
    }

    /**
     *
     * @param id book comment id
     * @return
     */
    public static String[] getContents(String id) {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfBookComment);
        FindIterable<Document> findIterable = collection.find(Filters.eq("_id", new ObjectId(id)))
                .projection(Projections.include("title", "content"));
        MongoCursor<Document> cursor = findIterable.iterator();

        String[] values = new String[2];
        if (cursor.hasNext()) {
            Document doc = cursor.next();
            values[0] = doc.get("title").toString();
            values[1] = doc.get("content").toString();
        }
        cursor.close();

        return values;
    }
    // update
    public static String update(String title, String content, String id) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String time = now.format(formatter);

        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfBookComment);
        Bson filter = Filters.eq("_id", new ObjectId(id));
        Bson update = Updates.combine(Updates.set("title", title),
                Updates.set("content", content),
                Updates.set("updatetime", time));
        collection.updateOne(filter, update);

        return id;
    }

    // delete

    // get all
    public static List<String[]> getAll() {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfBookComment);
        FindIterable<Document> findIterable = collection.find(Filters.eq("submit", true));
        MongoCursor<Document> cursor = findIterable.iterator();

        List<String[]> result = new ArrayList<>();
        while(cursor.hasNext()) {
            Document doc = cursor.next();

            String[] strs = new String[3];
            strs[0] = doc.get("_id").toString();
            strs[1] = doc.get("title").toString();
            strs[2] = doc.get("submittime").toString();

            result.add(strs);
        }
        cursor.close();

        return result;
    }
}
